package com.gyp.utils;


import java.security.MessageDigest;

public class Sha256Util {
    public Sha256Util() {
    }

    public static void main(String[] args) {
        try {
            System.out.println(getSHA256("160001034463c712548b053ec6c83fd2e4cfb88e51b9").toUpperCase().substring(0, 32));
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public static String getSHA256(String str) throws Exception {
        String encodestr = "";
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(str.getBytes("UTF-8"));
        byte[] aa = messageDigest.digest();
        encodestr = byte2Hex(aa);
        return encodestr;
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;

        for(int i = 0; i < bytes.length; ++i) {
            temp = Integer.toHexString(bytes[i] & 255);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }

            stringBuffer.append(temp);
        }

        return stringBuffer.toString();
    }
}
